package com.yuzhi.master.sewage.wmst.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.common.util.StringUtils;
import com.yuzhi.master.sewage.wmst.domain.DrainWmst;
import com.yuzhi.master.sewage.wmst.domain.bo.DrainWmstBo;
import com.yuzhi.master.sewage.wmst.domain.request.DrainWmstReq;
import com.yuzhi.master.sewage.wmst.domain.vo.DrainWmstVo;
import com.yuzhi.master.sewage.wmst.mapper.DrainWmstMapper;
import com.yuzhi.master.sewage.wmst.service.DrainWmstService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DrainWmstServiceImpl implements DrainWmstService {

    @Autowired
    private DrainWmstMapper drainWmstMapper;

    @Override
    public PageDataInfo<DrainWmstVo> queryPageList(DrainWmstReq request) {
        LambdaQueryWrapper<DrainWmst> lqw = buildQueryWrapper(request);
        Page<DrainWmstVo> result = drainWmstMapper.selectVoPage(request.getPageQuery().build(),lqw);
        return PageDataInfo.build(result);
    }


    private LambdaQueryWrapper<DrainWmst> buildQueryWrapper(DrainWmstReq request) {
        Map<String, Object> params = request.getParams();
        LambdaQueryWrapper<DrainWmst> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(request.getEngType()), DrainWmst::getEngType, request.getEngType());
        lqw.eq(StringUtils.isNotBlank(request.getFkCd()), DrainWmst::getFkCd, request.getFkCd());
        lqw.like(StringUtils.isNotBlank(request.getWmstName()), DrainWmst::getWmstName, request.getWmstName());
        return lqw;
    }

    @Override
    public List<DrainWmstVo> queryWmstVo(DrainWmstReq request)
    {
        LambdaQueryWrapper<DrainWmst> lqw = buildQueryWrapper(request);
        List<DrainWmstVo>  voList  = drainWmstMapper.selectVoList(lqw);
        return voList;
    }

    /**
     * 新增视频设备
     * @param bo
     * @return
     */
    @Override
    public Boolean addDrainWmst(DrainWmstBo bo)
    {
        DrainWmst add = BeanUtil.toBean(bo, DrainWmst.class);
        return  drainWmstMapper.insert(add)>0;
    }

    /**
     * 修改视频设备
     * @param bo
     * @return
     */
    @Override
    public Boolean updateByBo(DrainWmstBo bo)
    {
        DrainWmst update = BeanUtil.toBean(bo, DrainWmst.class);

        return drainWmstMapper.updateById(update) > 0;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public  DrainWmstVo queryById(String id)
    {
        DrainWmstVo  vo =  drainWmstMapper.selectVoById(id);
        return vo;
    }

    /**
     * 删除
     * @param id
     * @param isValid
     * @return
     */
    public Boolean deleteById(String id, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return drainWmstMapper.deleteById(id) > 0;
    }


}
